/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.reporter;

import za.org.foobar.famon.util.Util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPOutputStream;

/**
 *
 */
public class HttpClient {

    // Saved response.
    private java.util.Map<String,java.util.List<String>> responseHeader = null;
    private java.net.URL responseURL = null;
    private int responseCode = -1;
    private String MIMEtype  = null;
    private String content   = null;

    public HttpClient(String urlString, String data, int timeoutSeconds) throws java.io.IOException {
        // Open a URL connection.
        final URL url = new URL(urlString);
        final URLConnection uconn = url.openConnection();
        if (!(uconn instanceof HttpURLConnection)) {
            throw new IllegalArgumentException("URL protocol must be HTTP.");
        }
        final HttpURLConnection conn =(HttpURLConnection)uconn;

        // Set up a request.
        conn.setConnectTimeout(timeoutSeconds * 1000);
        conn.setReadTimeout(timeoutSeconds * 1000);
        conn.setInstanceFollowRedirects(true);
//        conn.setRequestProperty("User-Agent", "Mozilla/4.0");
        conn.setRequestProperty("Content-Type", "application/octet-stream");
        conn.setRequestProperty("Content-Coding", "gzip");
        //conn.setRequestProperty("Content-Length", Integer.toString(data.length()));

        conn.setDoOutput(true);
        OutputStream os = conn.getOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(os);
        gos.write(data.getBytes());
        gos.close();
        // Send the request.
        conn.connect();

        // Get the response.
        responseHeader = conn.getHeaderFields();
        responseCode = conn.getResponseCode();
        responseURL = conn.getURL();
        final int length = conn.getContentLength();

        if (length > 0) {
            // Get the content.
            InputStream stream = conn.getErrorStream();
            if (stream == null) {
                stream = conn.getInputStream();
            }
            if (stream != null) {
                content = Util.readFile(stream);
            }
        }
        conn.disconnect();
    }


    /** Get the content. */
    public Object getContent() {
        return content;
    }

    /** Get the response code. */
    public int getResponseCode() {
        return responseCode;
    }

    /** Get the response header. */
    public java.util.Map<String,java.util.List<String>> getHeaderFields() {
        return responseHeader;
    }

    /** Get the URL of the received page. */
    public URL getURL() {
        return responseURL;
    }

    /** Get the MIME type. */
    public String getMIMEType() {
        return MIMEtype;
    }

    public boolean isResponseOK() {
        return responseCode == HttpURLConnection.HTTP_OK;
    }
}


